/*
 * Copyright 2017-2020 Wuwei Tengyu Logistics Center Co., Ltd.
 */

package com.goblin.common.response;

import java.io.Serializable;

import lombok.Data;

/**
 * desc: .
 *
 * @param <T> data class
 * @author go blin
 * <p>
 * 2019/6/3 15:41
 */

@Data
public final class Response<T> implements Serializable {

	private static final long serialVersionUID = -4898623024209777274L;
	private String code;
	private String message;
	private T data;

	public static final String SUCCESS = "成功";

	public static final String FAIL = "失败";

	public static <T> Response success(T data) {
		Response result = new Response();
		result.setCode(Code.SUCCESS.value);
		result.setMessage(SUCCESS);
		result.setData(data);
		return result;
	}

	public static <T> Response success() {
		Response result = new Response();
		result.setCode(Code.SUCCESS.value);
		result.setMessage(Message.SUCCESS.value);
		result.setData(null);
		return result;
	}

	public static Response failure(Code code, Message message) {
		Response result = new Response();
		result.setCode(code.value);
		result.setMessage(message.value);
		result.setData(null);
		return result;
	}

	public static Response failure(Code code, String message) {
		Response result = new Response();
		result.setCode(code.value);
		result.setMessage(message);
		result.setData(null);
		return result;
	}

	public static <T> Response failure(Code code, Message message, T data) {
		Response result = new Response();
		result.setCode(code.value);
		result.setMessage(message.value);
		result.setData(data);
		return result;
	}

	@Override
	public String toString() {
		return "Response{" + "code='" + code + '\'' + ", message='" + message + '\''
				+ ", data=" + data + '}';
	}
}
